package org.aphelie.torg.ruler.internal.ui.editor;

import org.aphelie.torg.ruler.internal.ui.parts.FormEntry;
import org.aphelie.torg.ruler.internal.ui.parts.IFormEntryListener;
import org.eclipse.jface.action.IStatusLineManager;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.text.TextSelection;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.forms.events.HyperlinkEvent;

public class FormEntryAdapter implements IFormEntryListener {
	private IContextPart contextPart;
	protected IActionBars actionBars;
	
	public FormEntryAdapter(IContextPart contextPart) {
		this(contextPart, null);
	}
	
	public FormEntryAdapter(IContextPart contextPart, IActionBars actionBars) {
		this.contextPart = contextPart;
		this.actionBars = actionBars;
	}
	
	@Override
	public void focusGained(FormEntry entry) {
		ITextSelection selection = new TextSelection(1, 1);
		contextPart.getPage().getTGREditor().getContributor().updateSelectableActions(selection);
	}
	
	@Override
	public void textDirty(FormEntry entry) {
		contextPart.fireSaveNeeded();
	}
	
	@Override
	public void textValueChanged(FormEntry entry) {
	}
	
    public void browseButtonSelected(FormEntry entry) {
    }	

	@Override
	public void linkEntered(HyperlinkEvent e) {
		if (actionBars == null)
			return;
		IStatusLineManager mng = actionBars.getStatusLineManager();
		mng.setMessage(e.getLabel());
	}

	@Override
	public void linkExited(HyperlinkEvent e) {
		if (actionBars == null)
			return;
		IStatusLineManager mng = actionBars.getStatusLineManager();
		mng.setMessage(null);		
	}

	@Override
	public void linkActivated(HyperlinkEvent e) {
	}

	@Override
	public void selectionChanged(FormEntry entry) {
		ITextSelection selection = new TextSelection(1, 1);
		contextPart.getPage().getTGREditor().getContributor().updateSelectableActions(selection);
	}
}
